package com.fingard.dsp.bank.directbank.smbc01;

import com.fingard.constant.Format;
import com.fingard.dsp.bank.directbank.bankInterface.IFileResolveDifference;
import com.fingard.io.FileHelper;
import com.fingard.text.StringHelper;

import java.io.File;
import java.util.*;

public class SMBC01ForeignBank extends SMBC01Base implements IFileResolveDifference {
    /**
     * MT942中获取期初余额，当前余额，当前可用余额，上次交易后的余额
     * @return
     */
    public Map<String,String> getBalanceMap(String context, String accountNum, String date) throws Exception{
        return null;
    }

    /**
     * 解析86字符串获取对手信息：对方账号，对方户名，对方银行名称，附言
     * @param string86
     * @return
     * @throws Exception
     */
    public Map<String,String> getRecInformationMap(String string86){
        //定义元素
        String recAct = "";//对方账户
        String recName = "";//对方名称
        String recBankName="";//对方银行名称
        String recDetail = "";//备注
        String reqId="";//付款流水号
        String[] detailsplit = string86.split("/");
        List<String> list = Arrays.asList(detailsplit);
        //对方账户
        if (list.indexOf("PA") != -1) {
            recAct = list.get(list.indexOf("PA") + 1).trim();
            recAct = recAct.replaceAll("\r\n", "");
        } else {
            //借贷标识为C时，银行未返回对方账户
            recAct = "";
        }
        //对方名称
        if (list.indexOf("PN") != -1) {
            recName = list.get(list.indexOf("PN") + 1).trim();
            recName = recName.replaceAll("\r\n", "");
        }
        //对方银行名称
        if (list.indexOf("PB") != -1) {
            recBankName = list.get(list.indexOf("PB") + 1).trim();
            recBankName = recBankName.replaceAll("\r\n", "");
        }
        //用途
        if (list.indexOf("OD") != -1) {
            recDetail = list.get(list.indexOf("OD") + 1).trim();
            recDetail = recDetail.replaceAll("\r\n", "");
        }
        if(list.indexOf("RM1") != -1){
            reqId = list.get(list.indexOf("RM1") + 1).trim();
            reqId = reqId.replaceAll("\r\n", "");
        }
        //返回值
        Map<String,String> map=new HashMap<String, String>();
        map.put("recAct",recAct);
        map.put("recName",recName);
        map.put("recBankName",recBankName);
        map.put("recDetail",recDetail);
        map.put("reqId",reqId);
        return map;
    }

    /**
     * 解析付款响应文件
     * @param filePath
     * @throws Exception
     */
    @Override
    public void analysisAckFile(String filePath) throws Exception {
        String ackFilePath=StringHelper.trimEndFileSp(getBankConfig().getValueAsString("DealedPath"));
        String[] rspStatus=new String[]{"","",""};
        if(FileHelper.existFile(filePath)){
            String content=FileHelper.readFileText(filePath,getCharset());
            WriteBankLogLn("获取ack文件内容为："+content+"开始处理");
            String fileName=new File(filePath).getName();
            fileName=fileName.replaceAll(".csv","");
            String[] nameAry=fileName.split("_");
            if(nameAry.length==4){
                //支付流水号
                rspStatus[0]=nameAry[3];
                //支付时间
                String payDate=Format.DateYear4.format(new Date()).substring(0,2) +nameAry[0].substring(nameAry[0].length()-6);
                //格式化
                String ackContext=content.replaceAll("\r","").replaceAll("\n","").replaceAll("\t","");
                String[] resultAry=ackContext.split(",");
                //支付状态
                rspStatus[1]=resultAry[resultAry.length-1].trim();//取最后一个值
                rspStatus[2]=rspStatus[1];
                //生成ack处理文件
                getAckStatus(ackFilePath,payDate,rspStatus,"1");
                WriteBankLogLn("ack-Lv.1文件已处理");
            }else {
                WriteBankLogLn("ack文件"+filePath+"名字格式异常");
                //throw new Exception("ack文件名字格式异常");
            }
        }else{
            WriteBankLogLn("文件"+filePath+"不存在");
            //throw new Exception("文件"+filePath+"不存在");
        }
    }
}
